package com.class5.springboot.service.real;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.OrgInfo;
import com.class5.springboot.entity.OrgUser;
import com.class5.springboot.entity.TbXtUser;
import com.class5.springboot.entity.code.SysUserStatus;
import com.class5.springboot.entity.vo.TbXtUserVo;
import com.class5.springboot.frame.context.SuperServiceImpl;
import com.class5.springboot.frame.logger.ILoginLoggerService;
import com.class5.springboot.service.facade.ILoginService;
import com.class5.springboot.util.BeanCopyUtils;
import com.class5.springboot.util.Md5Util;
import com.class5.springboot.util.exception.AppException;
import com.class5.springboot.util.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService extends SuperServiceImpl implements ILoginService{

    @Autowired
    private ILoginLoggerService loginLoggerService;

    @Override
    public TbXtUserVo login(String username, String password, String ip) {
        checkNotEmpties(username, "帐号", password, "密码");
        username = username.toLowerCase().trim();
        password = password.trim();
        TbXtUser user = repo.getUnique("user_name", username, TbXtUser.class);
        if(user == null) {
            throw new ServiceException("帐号不存在");
        }
        if(!user.getStatus().equals(SysUserStatus.NORMAL)) {
            throw new ServiceException("帐号暂不能使用");
        }
        System.out.println(Md5Util.md5Hex(username+password));
        System.out.println(user.getPassword());
        if(!Md5Util.md5Hex(username+password).equals(user.getPassword())) {
            throw new ServiceException("密码不正确");
        }
        TbXtUserVo vo = new TbXtUserVo();
        getTbXtUserVoData(vo,user);
        OrgInfo orgInfo = repo.get(user.getOrgId(),OrgInfo.class);
        vo.setOrgName(orgInfo.getName());
        //记录日志
        loginLoggerService.create(user.getId(), user.getOrgId(), ip, "web");
        return vo;
    }

    private void getTbXtUserVoData(TbXtUserVo vo, TbXtUser user) {
        BeanCopyUtils.copyNoEqClass(user,vo);
    }

    @Override
    public void register(JSONObject param) {
        String username = param.getString("userName").toLowerCase().trim();
        String password = param.getString("passWord").trim();
        String userrole = param.getString("userRole").trim();
        String phone = param.getString("phone").trim();
        String email = param.getString("email").trim();
        String sex = param.getString("sex").trim();
        String realname = param.getString("realName").trim();
        TbXtUser xtUser = repo.getUnique("user_name",username,TbXtUser.class);
        if(xtUser != null){
            throw new AppException("该账户已存在");
        }
        xtUser = new TbXtUser();
        xtUser.setUserName(username);
        xtUser.setPassword(Md5Util.md5Hex(username+password));
        xtUser.setOrgId(Long.valueOf("1"));
        xtUser.setRoleId(userrole);
        xtUser.setRealName(realname);
        xtUser.setStatus(SysUserStatus.NORMAL);
        xtUser.setSex(sex);
        xtUser.setPhone(phone);
        xtUser.setEmail(email);
        repo.insert(xtUser);
    }

    public static void main(String[] args) {
        System.out.println( Md5Util.md5Hex("admin"+"1"));
        System.out.println( Md5Util.md5Hex("admin"+"123456"));
        System.out.println( Md5Util.md5Hex("zhangsan"+"2335344"));
    }
}
